using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class InUserDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<InUser> GetInUserList()
        {
            try
            {
                const string spName = "spi_InUser_GetList";

                var InUserList = DbHelper.ExecuteSPList<InUser>(spName);
                return InUserList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<InUser>();
            }
        }

        //public List<InUser> GetInUserListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        //{
        //    try
        //    {
        //        const string spName = "spi_InUser_GetByPaging";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@PageIndex", pageIndex),
        //                                new SqlParameter("@PageSize", pageSize),
        //                                new SqlParameter("@TotalRecord", totalRecord)
        //                            };

        //        SqlCommand sqlCommand = new SqlCommand(spName);
        //        sqlCommand.CommandType = CommandType.StoredProcedure;
        //        sqlCommand.CommandText = spName;
        //        sqlCommand.Parameters.AddRange(sqlParams);
        //        sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
        //        var InUserList = DbHelper.ExecuteSqlList<InUser>(sqlCommand);
        //        if (sqlCommand.Parameters["@TotalRecord"].Value != null)
        //        {
        //            totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
        //        }

        //        return InUserList;
        //    }
        //    catch (Exception exp)
        //    {
        //        LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

        //        return new List<InUser>();
        //    }
        //}

        public List<InUser> GetInUserListSearch(InUser InUser)
        {
            try
            {
                const string spName = "spi_InUser_GetListSearch";

                var sqlParams = new[]
                                    {
                                        new SqlParameter("@UserMail", InUser.UserMail),
                                        new SqlParameter("@GroupId", InUser.GroupId),
                                        new SqlParameter("@UserIdentity", InUser.UserIdentity),
                                        new SqlParameter("@UserPhone", InUser.UserPhone),
                                        new SqlParameter("@UserFullname", InUser.UserFullname),
                                        new SqlParameter("@UserStatus", InUser.UserStatus)

                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);

                var InUserList = DbHelper.ExecuteSqlList<InUser>(sqlCommand);
                return InUserList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<InUser>();
            }
        }

        public List<InUser> GetInUserListByGroupId(int groupid)
        {
            try
            {
                const string spName = "spi_InUser_GetListByGroupId";

                var sqlParams = new[]
                                    {
                                        new SqlParameter("@GroupId", groupid)
                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);

                var InUserList = DbHelper.ExecuteSqlList<InUser>(sqlCommand);
                return InUserList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<InUser>();
            }
        }

        public InUser GetInUserById(int userId)
        {
            try
            {
                const string spName = "spi_InUser_GetById";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@UserId", userId)
                                    };

                var InUser = DbHelper.ExecuteSPList<InUser>(spName, sqlParams).SingleOrDefault();
                return InUser;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return null;
            }
        }

        public int InsertInUser(InUser InUser)
        {
            try
            {
                const string spName = "spi_InUser_Insert";
                var sqlParams = new[]
                                    {

                                        new SqlParameter("@GroupId", InUser.GroupId),
                                        new SqlParameter("@UserName", InUser.UserName),
                                        new SqlParameter("@UserPass", InUser.UserPass),
                                        new SqlParameter("@UserMail", InUser.UserMail),
                                        new SqlParameter("@UserPhone", InUser.UserPhone),
                                        new SqlParameter("@UserFullname", InUser.UserFullname),
                                        new SqlParameter("@UserBirthday", InUser.UserBirthday),
                                        new SqlParameter("@UserAvatar", InUser.UserAvatar),
                                        new SqlParameter("@UserIdentity", InUser.UserIdentity),
                                        new SqlParameter("@IsSuperAdmin", InUser.IsSuperAdmin),
                                        new SqlParameter("@UserStatus", InUser.UserStatus),
                                        new SqlParameter("@UserId", InUser.UserId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@UserId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@UserId"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@UserId"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int UpdateInUser(InUser InUser)
        {
            try
            {
                const string spName = "spi_InUser_Update";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@UserId", InUser.UserId)
                                        , new SqlParameter("@GroupId", InUser.GroupId)
                                        , new SqlParameter("@UserName", InUser.UserName)
                                        , new SqlParameter("@UserPass", InUser.UserPass)
                                        , new SqlParameter("@UserMail", InUser.UserMail)
                                        , new SqlParameter("@UserPhone", InUser.UserPhone)
                                        , new SqlParameter("@UserFullname", InUser.UserFullname)
                                        , new SqlParameter("@UserBirthday", InUser.UserBirthday)
                                        , new SqlParameter("@UserAvatar", InUser.UserAvatar)
                                        , new SqlParameter("@UserIdentity", InUser.UserIdentity)
                                        , new SqlParameter("@UserStatus", InUser.UserStatus)
                                        , new SqlParameter("@IsSuperAdmin", InUser.IsSuperAdmin)
                                        
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int DeleteInUser(int userId)
        {
            try
            {
                const string spName = "spi_InUser_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@UserId", userId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int CheckInUserEmailExist(string email)
        {
            try
            {
                Int32 cnt = 0;
                const string spName = "spi_InUser_CheckEmail";
                var sqlParams = new[]
                                    {

										new SqlParameter("@UserMail", email),
										new SqlParameter("@Cnt", cnt),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@Cnt"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@Cnt"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@Cnt"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public InUser CheckLogin(string email, string pass)
        {
            try
            {
                const string spName = "spi_InUser_GetByEmailAndPass";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@UserMail", email),
                                        new SqlParameter("@UserPass", pass)
                                    };

                var InUser = DbHelper.ExecuteSPList<InUser>(spName, sqlParams).SingleOrDefault();
                return InUser;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return null;
            }
        }
		#endregion
    }
}
